/**
 * finish-release
 */
import ColorString from "./color-string.js";

function finish_release() {
    //
    const root = process.cwd();
    const packageJsonPath = path.resolve(root, "package.json");
    fs.readFile(packageJsonPath, "utf8", (_, data) => {
        _process_data(data);
    });

    /**
     *
     * @param {string} data
     */
    function _process_data(data) {
        //
        try {
            console.log(`\n`);
            console.log(`\x1b[32m正在发布，请耐心等待 .... \x1b[0m\n`);
            const packageJson = JSON.parse(data);
            const _version = `v${packageJson.version}`;
            const _message_t = `chore(release): ${_version} 🏆`;

            const _command1 = `git commit -m "${_message_t}" --allow-empty`;
            const _command2 = `git tag ${_version} -m "${_message_t}" --force`;
            const _command3 = `git push --follow-tags origin master`;

            // console.log(`\x1b[32mcommand: ${_command1}\x1b[0m\n`)
            exec(_command1, (error, stdout, stderr) => {
                if (stderr) {
                    console.warn(stderr);
                }
                if (stdout) {
                    console.log(`\x1b[32m${stdout}\x1b[0m`);
                }
                exec(_command2, (error, stdout, stderr) => {
                    if (stderr) {
                        console.warn(stderr);
                    }
                    if (stdout) {
                        console.log(`\x1b[32m${stdout}\x1b[0m`);
                    }
                    exec(_command3, (error, stdout, stderr) => {});
                    if (stderr) {
                        console.warn(stderr);
                    }
                    if (stdout) {
                        console.log(`\x1b[32m${stdout}\x1b[0m`);
                    }
                    console.log(`\x1b[32m发布成功！\x1b[0m\n`);
                });
            });
        } catch (error) {
            console.log(error);
        }
    }
}
